package com.isale.web.sys.controller;

import com.isale.common.json.AjaxUtil;
import com.isale.common.model.sso.Descp;
import com.isale.model.db.SysRole;
import com.isale.service.SysRoleService;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 数据库表【sys_role】对应的controller对象
 *
 * @author bjs
 * @date 2016-11-22 15:18:06
 */

@Controller
@RequestMapping("/sys/sys_role/")
public class SysRoleController {

    private static final Log logger = LogFactory.getLog(SysRoleController.class);


    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 列表页面
     *
     * @param model
     * @return
     */
    @Descp(name = "[]列表页面")
    @RequestMapping("list-sys_role.html")
    public String list(Model model) {
        return "sys/sys_role/list-sys_role";
    }


    /**
     * 加载列表数据ajax
     *
     * @param page
     * @param rows
     * @return
     */
    @Descp(name = "[]列表ajax请求")
    @RequestMapping("list-sys_role.ajax")
    @ResponseBody
    public JSONObject list_data(
//            String name,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "rows", defaultValue = "20") int rows) {
        Pageable pageable = new PageRequest(page - 1, rows);
        Page<SysRole> pageRst = sysRoleService.findForAdminList(pageable);


        return AjaxUtil.jqGridJson(pageRst);
    }


    /**
     * 添加页面
     *
     * @param model
     * @return
     */
    @Descp(name = "[]添加页面")
    @RequestMapping("add-sys_role.html")
    public String add(Model model) {
        return "sys/sys_role/add-sys_role";
    }


    /**
     * 添加保存的ajax
     *
     * @param sysRole
     * @return
     */
    @RequestMapping("save-sys_role.ajax")
    @ResponseBody
    public JSONObject save(SysRole sysRole) {
        try {
            sysRoleService.save(sysRole);
            return AjaxUtil.success(null, sysRole);
        } catch (Exception ex) {
            logger.error("添加保存的ajax，sysRole=>" + sysRole, ex);
            return AjaxUtil.failure(ex.getMessage());
        }
    }


    /**
     * 修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @Descp(name = "[]修改页面")
    @RequestMapping("edit-sys_role.html")
    public String edit(Long id, Model model) {
        SysRole sysRole = sysRoleService.findById(id);
        model.addAttribute("sysRole", sysRole);
        return "sys/sys_role/edit-sys_role";
    }


    /**
     * 修改保存的ajax
     *
     * @param sysRole
     * @return
     */
    @Descp(name = "[]修改ajax请求")
    @RequestMapping("update-sys_role.ajax")
    @ResponseBody
    public JSONObject update(SysRole sysRole) {
        try {
            sysRoleService.save(sysRole);
            return AjaxUtil.success(null, sysRole);
        } catch (Exception ex) {
            logger.error("修改保存的ajax，sysRole=>" + sysRole, ex);
            return AjaxUtil.failure(ex.getMessage());
        }
    }


}
